package com.zx.idc.backend.gui.base.ip;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.base.ip.service.BaseIpAddressInfoReportService;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.ds.base.ip.entity.BaseIpAddressInfo;
import com.zx.idc.ds.base.ip.service.IBaseIpAddressInfoService;
import com.zx.idc.ds.base.ip.vo.BaseIpAddressInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * ip 地址信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseIpAddressInfo")
public class BaseIpAddressInfoController extends BasicBaseApi<IBaseIpAddressInfoService, BaseIpAddressInfo> implements BaseExcelApi<IBaseIpAddressInfoService, BaseIpAddressInfoVo> {
    @Autowired
    private BaseIpAddressInfoReportService baseIpAddressInfoReportService;

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-ip-address-info")
    public Result<Page<BaseIpAddressInfoVo>> page(Page<BaseIpAddressInfoVo> page, BaseIpAddressInfoVo baseIpAddressInfoVo) {
        return success(service.pageByParam(page, baseIpAddressInfoVo));
    }

    @GetMapping("/get/{id}")
    @RequiresPermissions("basic-data:base-ip-address-info")
    public Result<BaseIpAddressInfoVo> get(@PathVariable("id") Long id) {
        return success(service.getById(id));
    }

    @PostMapping("/insert-report/{id}")
    @RequiresPermissions("basic-data:base-ip-address-info:edit")
    public Result<Boolean> insertReport(@PathVariable Long id) {
        return success(baseIpAddressInfoReportService.insertReport(id));
    }

    @Override
    @RequiresPermissions("basic-data:base-ip-address-info:edit")
    public Result<Boolean> add(@RequestBody BaseIpAddressInfo entity) {
        String msg;
        try {
            return success(service.addIpAddressInfo(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("新增IP地址异常", e);
        }
        return error(msg);
    }

    @PostMapping("/update-report/{id}")
    @RequiresPermissions("basic-data:base-ip-address-info:edit")
    public Result<Boolean> updateReport(@PathVariable Long id) {
        return success(baseIpAddressInfoReportService.updateReport(id));
    }

    /**
     * 通用的更新方法
     *
     * @param entity 需要更新的实体对象
     * @return 更新是否成功，需要以 {@link Result#getData()} 作为判断标准
     */
    @Override
    @RequiresPermissions("basic-data:base-ip-address-info:edit")
    public Result<Boolean> update(@RequestBody BaseIpAddressInfo entity) {
        String msg;
        try {
            return success(service.updateIpAddressInfo(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("修改IP地址异常", e);
        }
        return error(msg);
    }

    /**
     * 通用的批量删除方法
     *
     * @param idList 实体主键列表
     * @return 批量删除是否成功，需要以 {@link Result#getData()} 作为判断标准
     */
    @Override
    @RequestMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-ip-address-info:edit")
    public Result<Boolean> delete(@RequestBody List<Long> idList) {
        String msg;
        try {
            return success(service.deleteBatch(idList));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("删除IP地址异常", e);
        }
        return error(msg);
    }

}

